from PyQt4 import QtGui, QtCore

from add_plugins_widget import AddPlugins

class PluginManagerWidget(QtGui.QDialog):
    
    def __init__(self, parent):
        QtGui.QDialog.__init__(self, parent)
        self.pyTray = parent
        self.pluginFolder = parent.pytv.pluginFolder
        self._db = parent.db
        self._plugins = parent.pytv.plugins.get_plugins()
        self._actives = [p._active for p in self._plugins]
        self._lang = parent.lang
        self.setWindowTitle(self._lang['manager'])
        self.setFixedWidth(820)
        v_box = QtGui.QVBoxLayout(self)
        
        #Table
        self._table = QtGui.QTableWidget(1, 3)
        self._table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self._table.removeRow(0)
        self._table.setHorizontalHeaderLabels(self._lang['tablePlugin'])
        self._table.setColumnWidth(1, 210)
        self._table.setColumnWidth(2, 450)
        self._table.horizontalHeader().setStretchLastSection(True)
        self._set_data()
        
        #Buttons
        self._save = QtGui.QPushButton(self._lang['save'])
        self._cancel = QtGui.QPushButton(self._lang['cancel'])
        self._add = QtGui.QPushButton(self._lang['addPlugin'])
        h_box = QtGui.QHBoxLayout()
        h_box.addWidget(self._add)
        h_box.addWidget(self._cancel)
        h_box.addWidget(self._save)
        
        #Labels
        self._lblDescription = QtGui.QLabel(self._lang['description'])
        self._lblAuthors = QtGui.QLabel(self._lang['authors'])
        
        #add Widgets
        v_box.addWidget(self._table)
        v_box.addWidget(self._lblDescription)
        v_box.addWidget(self._lblAuthors)
        v_box.addLayout(h_box)
        
        #Signal->Slot
        self.connect(self._table, QtCore.SIGNAL("cellClicked(int, int)"), self._cell_data)
        self.connect(self._save, QtCore.SIGNAL("clicked()"), self._save_data)
        self.connect(self._cancel, QtCore.SIGNAL("clicked()"), self.hide)
        self.connect(self._add, QtCore.SIGNAL("clicked()"), self._add_new_plugins)
        
    def _set_data(self):
        r = 0
        for plug in self._plugins:
            self._table.insertRow(r)
            #Column 1
            imageFile = self.pluginFolder + plug._icon
            item = QtGui.QTableWidgetItem(QtGui.QIcon(imageFile), plug._name)
            item.setFlags( QtCore.Qt.ItemIsSelectable |  QtCore.Qt.ItemIsEnabled )
            self._table.setItem(r, 1, item)
            #Column 2
            item = QtGui.QTableWidgetItem(plug._description)
            item.setFlags( QtCore.Qt.ItemIsSelectable |  QtCore.Qt.ItemIsEnabled )
            self._table.setItem(r, 2, item)
            #Column 0
            item = QtGui.QTableWidgetItem('')
            self._table.setItem(r, 0, item)
            if plug._active:
                item.setCheckState(QtCore.Qt.Checked)
            else:
                item.setCheckState(QtCore.Qt.Unchecked)
            r += 1
            
    def _save_data(self):
        plugsList = []
        for i in range(self._table.rowCount()):
            val = 0 if self._actives[i] == False else 1
            plugsList.append([self._plugins[i]._name, val])
            self._plugins[i]._active = self._actives[i]
        self._db.update_plugins(plugsList)
        self.pyTray.load_menu_plugins()
        self.hide()
        
    def reload_data(self):
        self.pyTray.pytv.plugins.search_for_plugins()
        self._plugins = self.pyTray.pytv.plugins.get_plugins()
        self._actives = [p._active for p in self._plugins]
        while self._table.rowCount() > 0:
            self._table.removeRow(0)
        self._set_data()
            
    def _cell_data(self, row, col):
        self._lblDescription.setText(self._lang['description'] + self._plugins[row]._description)
        aut = ',\n\t'.join([a for a in self._plugins[row]._authors])
        self._lblAuthors.setText(self._lang['authors'] + aut)
        if col == 0 and self._plugins[row]._active:
            self._actives[row] = False
        elif col == 0:
            self._actives[row] = True
            
    def _add_new_plugins(self):
        addPlugs = AddPlugins(self, self._plugins, self.pluginFolder)
        addPlugs.show()

    def closeEvent(self, event):
        event.ignore()
        self.done(0)
